package org.bing.zion.handler;

import java.util.concurrent.atomic.AtomicLong;

import org.bing.zion.core.MessageHandler;
import org.bing.zion.core.MessageHandlerChain;
import org.bing.zion.core.Session;


public class CountHandler extends HandlerAdapter implements MessageHandler { 
	private AtomicLong num = new AtomicLong();
	private int interval;
	private long lastModify = 0;

	public CountHandler() {
		this(5000);
	}

	public CountHandler(int num) {
		this.interval = num;
	}

	public void doHandle(MessageHandlerChain chain, Session session, Object msg) {
		if (msg != null) {
			long l = num.incrementAndGet();
			if (lastModify == 0) {
				lastModify = System.currentTimeMillis();
			}
			if (l % interval == 0) {
				long now = System.currentTimeMillis();
				long time = (now - lastModify) / 1000;
				lastModify = now;
				logger.warn("Speed: " + interval / time + "/s, Total: " + l);
			}
		}
	}

}
